<?php

namespace App\Http\Controllers\redis;

use Illuminate\Support\Facades\Redis;

class RedisFunction
{
   public function __construct($db)
   {
 


      Redis::select($db);
   }


     /**
     * @description: redis储存
     * @param  string $key 储存值的字段
     * @param  string $val 储存值的字符串
     * @return bool   $res 保存是否成功
     */
    public function redisSet($key, $val,$time=false)
    {   
      // $expireKey = $key; 
      // //$key = $this->DbName.'_'.$key;
        $res = Redis::set($key, $val);
        !$time?'':  self::expire( $key, $time); //限时
        return $res;
    }
    /**
     * @description: redis  获取
     * @param string $key   字段
     * @return string $val  返回的值
     */
    public function redisGet($key)
    {  
      //$key = $this->DbName.'_'.$key;
        $val=  Redis::get($key);
        return $val;
    }
    /**
     * @description:  存哈希值(数组)成功返回1，失败返回0。若不存在会先创建表再赋值，若字段已存在会覆盖旧值
     * @param string $key  索引
     * @param array  $data 存的数组
     * @return bool  $res  是否存放成功
     */
    public function redishSet($key, $data , $time = false)
    {
      $expireKey = $key; 
      //$key = $this->DbName.'_'.$key;
         foreach ($data as $k => $val) {
            $res=     Redis::hset($key, $k, $val);
            if (!$res) {
                self::redishDel($key);     //添加失败时删除
                return $key.'字段的'.$k.'存放失败';
            }
        }
        !$time?'':  self::expire($expireKey,$time); //限时
        return true;
    }
   /**
    * @description:  设定过期时间
    * @param string $key  索引
    * @param int    $time 过期时间
    * @return {*}
    */ 
    public function expire($key,$time){
      //$key = $this->DbName.'_'.$key;
    Redis::expire($key,$time);
   }


    /**
     * @description: 哈希获取所有值
     * @param  string $key 索引
     * @return array
     */
    public function redishGetAll($key)
    {
      //$key = $this->DbName.'_'.$key;
        $val=    Redis::hGetAll($key);
        return $val;
    }
    /**
     * @description: 哈希删除所有元素
     * @param string $key 索引
     * @return {*}
     */
    public function redishDel($key)
    {
      //$key = $this->DbName.'_'.$key;
        $res=    Redis::hKeys($key);
        if (!empty($res)) {
            for ($i=0;$i<count($res);$i++) {
                Redis::hDel($key, $res[$i]);
            }
        }
    }
    /**
     * @description:  判断key是否存在
     * @param  string $key  索引
     * @return bool   $res  是否存在 true存在 false不存在
     */
    public function existSKey($key)
    {
      //$key = $this->DbName.'_'.$key;
        $res=   Redis::EXISTS($key);
        return $res;
    }

   /**
    * @description: 获取单独的哈希字段
    * @param string 索引
    * @param string 字段
    * @return bool
    */ 
    public function hGET($key,$field)
   {
    //$key = $this->DbName.'_'.$key;
      $res=   Redis::hGET($key,$field);
      return $res;
   } 


   /**
    * @description:   修改哈希值
    * @param  string $key   钥匙
    * @param  array  $field 要修改的字段
    * @param  int    $time  设置过期时间
    * @return bool   $res
    */
    public function hMset($key,$field,$time = null )
   {
    //$key = $this->DbName.'_'.$key;
    $res=   Redis::hMset($key,$field);
    !$time?:self::expire($key,$time);
    return $res;
   }
    
  /**
   * @description: 哈希多个字段一起拿取
   * @param  string $key    
   * @param  array  $field  字段名
   * @return mixed  $res    查询的信息 
   */ 
  public function hMget($key,$field)
  {
    //$key = $this->DbName.'_'.$key;
    $res=   Redis::hMget($key,$field);
    // array_search()
  
    return hMgetToArray($res,$field);
  }
  /**
   * @description: 清处单条缓存
   * @param {*} $key 
   * @return {*}
   */
  public function del($key)
  {
    //$key = $this->DbName.'_'.$key;
    $res=  Redis::del($key);
    return $res;
  }

   /**
   * @description:  判断缓存 哈希是否存在
   * @param  string $key  键值
   * @param  string $hkey 哈希键值
   * @return bool   $res
   */  
  public function hExists($key,$hKey){
    //$key = $this->DbName.'_'.$key;
    $res=  Redis::hExists($key,$hKey);
    return $res;
   }
 
   public function reName($oldKey,$newKey){
    $oldKey = $this->DbName.'_'.$oldKey;
    $newKey = $this->DbName.'_'.$newKey;
    $res=  Redis::Rename( $oldKey, $newKey);
    return $res;
   } 


   /**
    * @description:  删除哈希键值
    * @param string $key  键值
    * @param string $hKey 哈希键值
    * @return bool   $res
    */  
   public function hDel($key,$hKey){
    //$key = $this->DbName.'_'.$key;
     $res= Redis::hDel($key,$hKey);
     return $res;
   }


//   不需要区分
   /**
    * @description: 统计当前库数据
    * @param {*}
    * @return {*}
    */ 
   public function dbsize(){
    $res= Redis::dbsize();
    return $res;
  }
  /**
   * @description: 查看队列长度
   * @param {*}
   * @return {*}
   */
  public function llen($value){
    $res = Redis::llen($value);
    return $res;
  }
  /**
   * @description: 队列 头部添加元素
   * @param {*}
   * @return {*}
   */
  public function lpush($key,$value){
    $res = Redis::lpush($key,$value);
    return $res;
  }
  /**
   * @description: 队列 尾部部添加元素
   * @param {*}
   * @return {*}
   */
  public function rpush($key,$value,$time=null){
    $res = Redis::rpush($key,$value);
    !$time?:self::expire($key,$time);
    return $res;
  }

  public function  sismember($key,$value){
    $res = Redis::sismember($key,$value);
    return $res;
  }

   /**
   * @description: 添加客户集合
   * @param {*} $key 
   * @return {*}
   */
  public function sAdd($key,$value)
  {
    //$key = $this->DbName.'_'.$key;
    $res=  Redis::sAdd($key,$value);
    return $res;
  }

/**
   * @description: 获取集合所有元素
   * @param {*} $key 
   * @return {*}
   */
  public function smembers($key)
  {
    //$key = $this->DbName.'_'.$key;
    $res=  Redis::smembers($key);
    return $res;
  }

  //删除集合
  public function spop($key){
    $res=  Redis::spop($key);
    return $res;
  }
  

  //  s1 


   public function lRange($key,$start,$end){
    $res = Redis::lRange($key,$start,$end);
    return $res;
   }



  /**
   * @description:  移出并获取列表的第一个元素
   * @param {*} $key
   * @param {*} $timeout  等待超时时间  弹出
   * @return {*}
   */  
  public function blpop($key,$timeout = 0){
    $res = Redis::blpop($key,$timeout);
    return $res;
  }

  /**
   * @description: 清空所有缓存
   * @param {*}
   * @return {*}
   */
  public function flushAll()
  {
    Redis::flushAll();
  }
}

class Myface{
    public static function __callStatic($methodname, $arguments) {
        $classname=static::getFacadeClass();
        $DB = 0;
        $checkData = $arguments[count($arguments)-1];
        if(isset( $checkData['RedisDB'])){
            $DB =  $checkData['RedisDB'];
            unset($arguments[count($arguments)-1]);
        }
        if($classname){
            $obj=new $classname($DB);
        }
        return call_user_func_array([$obj, $methodname], $arguments);
    } 
}

class RedisController extends Myface{
  public  static function  getFacadeClass(){
      return 'App\Http\Controllers\redis\RedisFunction';
  }
}